package com.autumn.platform.core;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class Collections {

    private static final String COMMA_PATTERN = "\\s*(\\s|,)\\s*";

    @SuppressWarnings("unchecked")
    public static <T> List<T> asList(T... a) {
        return Arrays.asList(a);
    }

    @SuppressWarnings("unchecked")
    public static <T> Set<T> asSet(T... a) {
        Set<T> s = new LinkedHashSet<>();
        for (T t : a) {
            s.add(t);
        }
        return s;
    }

    /**
     * 使用逗号或空白分隔字符串
     * 
     * @param src
     * @return
     */
    public static List<String> asListSplitByComma(String src) {
        if (null == src) {
            return null;
        }
        String[] split = src.split(COMMA_PATTERN);
        List<String> s = new ArrayList<>();
        for (String sp : split) {
            s.add(sp);
        }
        return s;
    }

    /**
     * 使用逗号或空白分隔字符串
     * 
     * @param src
     * @return
     */
    public static Set<String> asSetSplitByComma(String src) {
        if (null == src) {
            return null;
        }
        String[] split = src.split(COMMA_PATTERN);
        Set<String> s = new LinkedHashSet<>();
        for (String sp : split) {
            s.add(sp);
        }
        return s;
    }
}
